package com.xujiang.data.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * http://www.jianshu.com/p/308f3c54abdd
 *
 * @author xujiang
 * @version 2017-07-20 10:55
 */
public class RetrofitUtils {
    private Retrofit mLongTimeRetrofit;

    private RetrofitUtils(){

    }

    private static RetrofitUtils retrofitUtils;

    public static RetrofitUtils get(){
        if(retrofitUtils == null){
            retrofitUtils = new RetrofitUtils();
        }
        return  retrofitUtils;
    }

    private Retrofit mRetrofit;
    private Retrofit.Builder mRetrofitBuilder;

    public void init(String baseUrl, OkHttpClient client){
        mRetrofitBuilder = new Retrofit.Builder().baseUrl(baseUrl).client(client)
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
        mRetrofit = mRetrofitBuilder.build();

        OkHttpClient.Builder builder = client.newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.MINUTES);
        mLongTimeRetrofit = RetrofitUtils.get().getRetrofitBuilder()
                .client(builder.build())
                .build();

    }

    public Retrofit.Builder getRetrofitBuilder() {
        return mRetrofitBuilder;
    }

    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    public Retrofit getLongTimeRetrofit() {
        return mLongTimeRetrofit;
    }


}
